package main

import (
	"context"
	"errors"
	"gitee.com/yms-zero/go-library/igin"
	"gitee.com/yms-zero/go-library/ilog"
	zeroConf "github.com/zeromicro/go-zero/core/conf"
	"go-api/app/global"
	"go-api/app/router"
	"go-api/conf"
	"os"
	"os/signal"
)

func main() {
	//读取配置文件
	var c conf.Conf
	zeroConf.MustLoad("config.yaml", &c)

	//日志
	log, err := ilog.NewLog(c.Log)
	if err != nil {
		panic(err)
	}

	//启动http
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	hs := igin.NewHttp(ctx, c.Http, log, igin.DefaultHttpMiddleware)

	G := global.NewGlobal(&c, hs.Log)

	go hs.WithOptions(router.Router(G)).Run()

	quit := make(chan os.Signal, 1)
	signal.Notify(quit, os.Interrupt)
	<-quit

	hs.Close(errors.New("quit")).Complete()
}

/*func sseHandler(w http.ResponseWriter, r *http.Request) {
	// 设置必要的 HTTP 头
	w.Header().Set("Content-Type", "text/event-stream")
	w.Header().Set("Cache-Control", "no-cache")
	w.Header().Set("Connection", "keep-alive")

	// 确保客户端可以接收实时数据
	flusher, ok := w.(http.Flusher)
	if !ok {
		http.Error(w, "Streaming unsupported!", http.StatusInternalServerError)
		return
	}

	// 持续发送数据到客户端
	for {
		// 向客户端发送当前时间
		fmt.Fprintf(w, "data: %s\n\n", "{\"choices\":[{\"delta\":{\"content\":null,\"role\":\"assistant\",\"reasoning_content\":\"\"},\"index\":0,\"logprobs\":null,\"finish_reason\":null}],\"object\":\"chat.completion.chunk\",\"usage\":null,\"created\":1740476143,\"system_fingerprint\":null,\"model\":\"deepseek-r1\",\"id\":\"chatcmpl-fe16824b-3c26-90f1-8511-59c8b628a023\"}")
		flusher.Flush() // 确保数据被立即发送到客户端

		// 模拟延迟
		time.Sleep(1 * time.Second)
	}
}

func main() {
	http.HandleFunc("/events", sseHandler)
	fmt.Println("Server started on :8080")
	http.ListenAndServe(":8080", nil)
}
*/
